package com.sdygmedia.action.kindeditor.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.action.kindeditor.util.FileUtil;

@Controller
public class UploadImageAction4 extends ActionSupport {

	private final static String UPLOAD = "/attached";

	private File imgFile;

	private String imgFileFileName;

	private String imgFileContentType;

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	@Override
	public String execute() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();

		String savePath = request.getSession().getServletContext().getRealPath(
				"/")
				+ "attached/";
		// �ļ�����Ŀ¼URL
		String saveUrl = request.getContextPath() + "/attached/";

		String dirName = request.getParameter("dir");

		if (dirName == null) {
			dirName = "image";
		}

		System.out.println("saveUrl---->" + saveUrl);

		String tpath = FileUtil.mkdirs(savePath, dirName);

		savePath += tpath;
		saveUrl += tpath;

		/*
		 * System.out.println("dir:"+dirName);
		 * 
		 * savePath += dirName + "/"; saveUrl += dirName + "/"; File saveDirFile
		 * = new File(savePath); if (!saveDirFile.exists()) {
		 * saveDirFile.mkdirs(); }
		 * 
		 * SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String ymd =
		 * sdf.format(new Date()); savePath += ymd + "/"; saveUrl += ymd + "/";
		 * File dirFile = new File(savePath); if (!dirFile.exists()) {
		 * dirFile.mkdirs(); }
		 * 
		 * System.out.println("saveUrl-----" + saveUrl);
		 * 
		 * System.out.println("savePath-----" + savePath);
		 */
		/*
		 * String fileName = imgFileFileName;
		 * 
		 * String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1)
		 * .toLowerCase();
		 * 
		 * SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String
		 * newFileName = df.format(new Date()) + "_" + new
		 * Random().nextInt(1000) + "." + fileExt;
		 */

		String newFileName = FileUtil.createNewFileName(imgFileFileName);

		InputStream in = new FileInputStream(imgFile);
		String dir = ServletActionContext.getRequest().getRealPath(UPLOAD);
		System.out.println(dir);

		OutputStream fos = new FileOutputStream(savePath + newFileName);

	

		byte[] buffer = new byte[1024 * 1024];

		int length = 0;

		while (-1 != (length = in.read(buffer))) {
			fos.write(buffer, 0, length);

		}
		fos.close();
		in.close();

		JSONObject obj = new JSONObject();
		obj.put("error", 0);
		obj.put("url", saveUrl + newFileName);
		

		ServletActionContext.getRequest().setAttribute("msg",
				obj.toJSONString());

		return "success4";
	}

}
